package com.gzdh.dishrecognition.ui.fragment.sysinfo;

import static com.dhkj.web.utils.DataUtils.type;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.dhkj.web.utils.MyObserverUZip;
import com.dhkj.web.utils.RequestUtils;
import com.greendao.dao.FkEntityDao;
import com.gzdh.dishrecognition.AppContext;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.FkEntity;
import com.gzdh.dishrecognition.model.FkModel;
import com.gzdh.dishrecognition.ui.adapter.DownFkAdapter;
import com.gzdh.dishrecognition.ui.adapter.FkInfoAdapter;
import com.gzdh.dishrecognition.ui.view.WaveProgressView;
import com.gzdh.dishrecognition.util.SystemUtils;

import java.util.ArrayList;
import java.util.List;

public class FkInfoFragment extends Fragment {


    private Context mContext;

    private Activity activity;

    private RecyclerView downRv;

    private Button downBut;

    private DownFkAdapter downFkAdapter;

    private WaveProgressView progress;

    private LinearLayout downLinear;

    private List<String> showList = new ArrayList<>();

    private FkEntityDao fkEntityDao;

    private TextView textTv;

    private RecyclerView recyclerView;
    private FkInfoAdapter fkInfoAdapter;

    public FkInfoFragment(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_fk_info, container, false);
        mContext = view.getContext();
        downRv = view.findViewById(R.id.downRv);
        downBut = view.findViewById(R.id.downBut);
        progress = view.findViewById(R.id.progress);
        textTv = view.findViewById(R.id.textTv);
        downFkAdapter = new DownFkAdapter(mContext);
        recyclerView = view.findViewById(R.id.recyclerView);
        fkEntityDao = AppContext.getInstance().getDaoSession().getFkEntityDao();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        downRv.setLayoutManager(layoutManager);
        downRv.setAdapter(downFkAdapter);
        downLinear = view.findViewById(R.id.downLinear);
        downBut.setOnClickListener(view1 -> {
            downLinear.setVisibility(View.VISIBLE);
            downFkAdapter.showLine("正在开始更新白名单");
            downFkAdapter.showLine("开始下载");
            downFk();
            fkEntityDao.deleteAll();

        });
        List<FkEntity> fkEntityList = fkEntityDao.loadAll();
        textTv.setText(String.format("白名单总数：%s", fkEntityList.size()));
        fkInfoAdapter = new FkInfoAdapter(mContext);
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager2);
        recyclerView.setAdapter(fkInfoAdapter);
        fkInfoAdapter.setList(fkEntityList);
        return view;
    }

    private int pageNum = 1;
    private int count = 0;

    private void downFk() {
        if (activity.isFinishing()) {
            return;
        }
        RequestUtils.getFk(mContext, String.valueOf(pageNum),
                "1000", SystemUtils.getSerialNumber(), new MyObserverUZip<>(mContext, type(JSONObject.class), false) {
                    @Override
                    public void onSuccess(JSONObject result) {
                        activity.runOnUiThread(() -> {
                            downFkAdapter.showLine("获取数据包1：" + result.getJSONArray("fk").size());
                            progress.setMax(result.getInteger("count"));
                        });
                        List<FkModel> fkModelList = result.getJSONArray("fk").toJavaList(FkModel.class);
                        List<FkEntity> fkEntityList = new ArrayList<>();
                        for (FkModel goo : fkModelList) {
                            FkEntity fkEntity = new FkEntity();
                            fkEntity.setCardno(goo.getCardno().trim());
                            fkEntity.setCardId(goo.getId().trim());
                            fkEntity.setCardsn(goo.getCardsn().toUpperCase().trim());
                            fkEntity.setName(goo.getName().trim());
                            fkEntity.setKye(goo.getKye());
                            fkEntity.setBtye(goo.getBtye());
                            fkEntity.setValiddate(goo.getValiddate("yyyy-MM-dd HH:mm:ss"));
                            fkEntity.setIsGuaShi(goo.getIsGuaShi() == null ? 0 : goo.getIsGuaShi());
                            fkEntityList.add(fkEntity);
                            activity.runOnUiThread(() -> {
                                downFkAdapter.showLine(goo.getCardno() + "    " + goo.getName() + "    " + goo.getCardsn());
                                progress.setProgress(++count);
                            });
                        }
                        fkEntityDao.insertInTx(fkEntityList);
                        activity.runOnUiThread(() -> {
                            if (fkModelList.size() == 1000) {
                                downFkAdapter.showLine("当前数据包是" + fkModelList.size());
                                downFkAdapter.showLine("正在下载下一个数据包");
                                pageNum++;
                                downFk();
                            } else {
                                downFkAdapter.showLine("共下载：" + pageNum + "包");
                                downFkAdapter.showLine("下载完成");
                            }
                        });
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        activity.runOnUiThread(() -> downFkAdapter.showLine(errorMsg));
                    }
                });
    }

}